package com.itcast.bihuan;

import com.itcast.util.DataClean;
import com.itcast.util.ExcelUtils;
import com.mongodb.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.UnknownHostException;
import java.util.List;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
public class findMongo {

    /**
     * 匹配mongodb中的数据过滤拿到闭环的数据
     * @param args
     */
    public static void main(String[] args){

        List<List<Object>>  list =  ExcelUtils.readExcelAll("d:\\js格式未闭环竞标标讯20160314.xlsx",1);

        //链接mongodb驱动
        MongoClient mongoClient = new MongoClient("localhost:27017");
        DB db = mongoClient.getDB("iris");
        DBCollection dbCollection = db.getCollection("data_1234");
        DBCursor dbCursor =  dbCollection.find();
        String ffp = "e:/zuizhong";
        String fileName = "bihuan.csv";
        File fileses = new File(ffp,fileName);
        int i=0;
        //循环访问各项数据
        while(dbCursor.hasNext()){
            DBObject dbObj= dbCursor.next();
            //这里读取一系列闭环的标查询看能不能查到
            //目前先搞一个吧
            String orgUrl = dbObj.get("orgUrl").toString();
            String contentAll = dbObj.get("content_all").toString();
            String title = "";
            if(null == dbObj.get("column1") ){
                title = "未抓取到标题";
            }else{
                title = dbObj.get("column1").toString();
            }
            //查看str里面有木有这个标
            for (List<Object> lis : list){
                boolean isHave = contentAll.contains(lis.get(1).toString());
                if(isHave){
                    try {
                       // CatchLinks.writeElement(fileses,lis.get(0).toString());
                        CatchLinks.writeElement(fileses,lis.get(1).toString());
                        //CatchLinks.writeElement(fileses,lis.get(2).toString());
                        CatchLinks.writeElement(fileses,orgUrl);
                        CatchLinks.writeElement(fileses, DataClean.clean(title));
                        //CatchLinks.writeElement(fileses,ContentExtractor.getContentByHtml(contentAll));
                        FileUtils.write(fileses, "\r\n", "GBK", true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(isHave + "" + i);
                    System.out.println( i + "---" + dbObj.get("_id").toString());
                    i++;
                }


            }
        }

    }
}
